function N= func_wrapper_serial_dict(principal_data,teacher_data ,store_suffix, rc_on, N      )



%% load principal prefs and clean them up


% this works: for a full string
folder='../../01_data/05_temp/';
fullmatfilename=fullfile(folder,principal_data);
load(fullmatfilename)

%load('../../01_data/05_temp/mixed_logit_principal_totVA_FRPL')
 

%load('../../01_data/05_temp/mixed_logit_principal_totVA_FRPL')

   
    IJX_princ=[ ones(length(IJX_princ),1) IJX_princ];   % add a vector of ones  
    IJ_princ=IJX_princ * thetahat(1:end-1,1);     % don't include RC
    clearvars -except IJ_princ IJN* IJVA* principal_data teacher_data store_suffix N rc_on year

    
folder='../../01_data/05_temp/';
fullmatfilename=fullfile(folder, teacher_data);
load(fullmatfilename)
%load('../../01_data/05_temp/mixed_logit_rc6_totVA_FRPL')


 
if rc_on==1
    % select the best Xdraws and clean them up
    [Xdraws_best] = func_likely_draws_current(IJitidx, IJchoiceset,IJcurrentFirst, Xdraws,Xmat,thetahat,y_logistic);
    
    % redo Xdraws_best to be as long as everything else
    [Ci, IAi, ICi]=unique(IJitidx(IJchoiceset==1));
    Xdraws_unique=Xdraws_best(IAi,:);
    Xdraws_best2=Xdraws_unique(IJitidx,:);
    
    % find predicted values in each match (teacher preferences)
    
    IJipref=[Xmat Xdraws_best2]*thetahat;
end

if rc_on==0
    
    IJipref=xb;
end
    
    % generate IJpref that is -999999999 if choiceset=0
    
    IJipref_choice=IJchoiceset.*IJipref + (1-IJchoiceset)*(-9999999999);
    
    % generate IJfracDisadv that is -99999999 if choicset=0
    
    IJfracDisadv_choice=IJchoiceset.*IJfracDisadv + (1-IJchoiceset)*(-9999999999);
 
    IJNdis= IJclassSize.*IJfracDisadv;
    IJNdis_choice=IJchoiceset.*IJNdis + (1-IJchoiceset)*(-9999999999);

    
    % build school preferences (by changing this block, can make it principal preferences, or....)
      
    IJoutput=IJclassSize.*IJVAma;
    IJoutput_dis=IJclassSize.*IJfracDisadv.*IJVAm2;
    IJoutput_adv=IJclassSize.*(1-IJfracDisadv).*IJVAm1;
    
  % generate ouput that is -999999999 if choiceset=0
    
    IJoutput_choice=IJchoiceset.*IJoutput + (1-IJchoiceset)*(-9999999999);   
    IJoutput_dis_choice=IJchoiceset.*IJoutput_dis + (1-IJchoiceset)*(-9999999999);   
    IJoutput_adv_choice=IJchoiceset.*IJoutput_adv + (1-IJchoiceset)*(-9999999999);   

    % generate IJ_princ that is -99999 if choiceset=0

    IJ_princ_choice=IJchoiceset.*IJ_princ + (1-IJchoiceset)*(-9999999999);  
    IJ_choice=(1-IJchoiceset)*(-9999999999);

    % generate principal for non-title I, and VA for title I
    
    IJ_princ_ntitleI_va_titleI=IJ_princ.*(IJfracDisadv<0.4) + IJoutput.*(IJfracDisadv>0.4)+IJoutput.*(IJfracDisadv==0.4);
    IJ_princ_titleI_va_ntitleI=IJ_princ.*(IJfracDisadv>0.4) + IJoutput.*(IJfracDisadv<0.4)+IJ_princ.*(IJfracDisadv==0.4);

    % generate 4 variables from the x matrix
    
    IJblack=Xmat(:,30);
    IJexp23=Xmat(:,33);
    IJexp46=Xmat(:,34);
    IJexp7p=Xmat(:,35);
    


%% 1. status quo, principal pref, restricted choice sets

balanced_store=zeros(N,7);



    for kk=1:N
       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1); 
       
               
      balanced_store(kk,:)= func_allocations_serialdict( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJ_princ_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJappyear,2015,1);
                  
    end 
    
  balanced=mean(balanced_store);
    
base=['alloc_serialdict_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    


end 



    
 